#include "public.h"
#include "uart.h"
#include "pwm.h"
#include "carctl.h"
#include "interrupt.h"
#include "buzzer.h"
#include "timer.h"
#include "interrupt.h"

void main()
{
	u8 recData = '\0';
	
	// 初始化串口
	uart_init();
	
	// 初始化pwm
	pwm_init((0xFF << 8) | 0xA4);
		
	while(1)
	{
		// 缓冲区中有指令则处理指令
		while(commendIndex != -1) 
		{
			recData = commendQueue[commendIndex--];

			buzzer_beep();
			
			// 加速
			if(recData == 'u')   
				car_speed_up();
			
			// 减速
			else if(recData == 'd') 
				car_speed_down();
			
			// 前进
			else if(recData == 'f')
				car_move_forward();
			
			// 左转
			else if(recData == 'l')
				car_turn_left();
			
			// 右转
			else if(recData == 'r') 
				car_turn_right();
					
			// 按键抬起, 小车停止移动
			else if(recData == 's')
				car_stop();
			
			// 右轮阈值减小
			else if(recData == '1')
			{
				rightWheelCompare--;
			}
			
			// 右轮阈值增大
			else if(recData == '2')
			{
				rightWheelCompare++;
			}
			
			// 左轮阈值减小
			else if(recData == '3')
			{
				leftWheelCompare--;
			}
			
			// 左轮阈值增大
			else if(recData == '4')
			{
				leftWheelCompare++;
			}
			
			// 蓝牙回显右轮阈值
			else if(recData == '5')
			{
				uart_send_byte(rightWheelCompare / 10 + '0');
				uart_send_byte(rightWheelCompare % 10 + '0');
			}
			
			// 蓝牙回显左轮阈值
			else if(recData == '6')
			{
				uart_send_byte(leftWheelCompare / 10 + '0');
				uart_send_byte(leftWheelCompare % 10 + '0');
			}
			
		}		
	}
}
